import axios from 'axios';
export default {

  // 企业微信通讯录组件鉴权
  wxAgentConfig({
    commit,
    state
  }, vThis) {
    const _companyId = window.localStorage.getItem('companyId');
    const _href = window.location.href;
    // const _href = 'https://web.seeklove.space/mobile/index'
    console.log('调用鉴权')
    return new Promise((resolve, reject) => {
      // 当不存在企业id时
      if (!_companyId) {
        reject({
          errMsg: '企业ID不存在'
        });
        return;
      }
      // 当存在vuex时
      if (window.WWOpenData && state.wxAgentConfigSuccess) {
        resolve();
        return;
      }

      !window.WWOpenData && vThis.dw.createJsApiScript(() => {

        axios({
          method: "post",
          url: "https://web.seeklove.space/prod-api/getJsSignAgent",
          params: {
            corp_id: _companyId,
            url: _href,
          },
          headers: {
            isToken: false
          },
        }).then((res) => {
          console.log(res);
          let resData = res.data.data;

          let dwSet = setTimeout(() => {

            vThis.dw.WxAgentConfigStart({
              corpid: resData.corpId,
              agentid: resData.agentId,
              timestamp: resData.timestamp,
              nonceStr: resData.nonceStr,
              signature: resData.signature,
              jsApiList: [
                'selectExternalContact',
                'startLiving',
                'replayLiving',
                'scanQRCode'
              ],
              onSuccess: () => {

                commit('SET_WX_AGENT_CONFIG_SUCCESS', true);
                resolve();
              },
              onFail: error => {

                reject(error);
              }
            });
            clearTimeout(dwSet)
          }, 800);
        });


      });
    });
  },

}
